//------------------------------------------------------------------------
// <copyright file="LookupController.cs" company="DevDir">
//   Copyleft (c) DevDir. No rights reserved.
// </copyright>
// <author>Nikita Sharov</author>
//------------------------------------------------------------------------

namespace DevDir.Mp3.Equilibrist
{
  using System;
  using System.Data;
  using System.Drawing;  
  using System.Windows.Forms;
  using System.ComponentModel;
  using System.Collections.ObjectModel;
  using MusicBrainz;
  using DevDir.Mp3.Equilibrist.Forms;
  using DevDir.Mp3.Equilibrist.Controls;
  
  /// <summary>
  /// Mighty LookupController...
  /// </summary>
  public class LookupController : IModule
  {
    private GenPuid genPuid = new GenPuid();
    private MetaDataTable newMetaDataTable = new MetaDataTable();
    private LookupControl control = new LookupControl();
    private ToolStripSeparator separator = new ToolStripSeparator();
    private ToolStripButton refreshButton = new ToolStripButton();
    private ToolStripButton processButton = new ToolStripButton();
    private ToolStripButton cancelButton = new ToolStripButton();

    /// <summary>
    /// Initializes a new instance of the <b>LookupController</b> class.
    /// </summary>
    public LookupController()
    {
      this.control.Dock = DockStyle.Fill;
      this.control.OriginalBindingSource.DataSource = 
        FileExplorer.Instance.Data;
      this.control.NewBindingSource.DataSource = this.newMetaDataTable;
      
      this.refreshButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
      this.refreshButton.Image = Properties.Resources.Refresh;
      this.refreshButton.ImageTransparentColor = Color.Magenta;
      this.refreshButton.Text = "Refresh";
      this.refreshButton.Click +=
        new System.EventHandler(this.RefreshButtonClick);

      this.processButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
      this.processButton.Image = Properties.Resources.Run;
      this.processButton.ImageTransparentColor = Color.Magenta;
      this.processButton.Text = "Process";
      this.processButton.Click += 
        new System.EventHandler(this.ProcessButtonClick);

      this.cancelButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
      this.cancelButton.Image = Properties.Resources.Cancel;
      this.cancelButton.ImageTransparentColor = Color.Magenta;
      this.cancelButton.Text = "Cancel";
      this.cancelButton.Click += 
        new System.EventHandler(this.CancelButtonClick);
    }

    public void Enter(MainForm form)
    {
      // HACK: Damn deep water.
      form.ToolStrip.Items.Add(this.separator);
      form.ToolStrip.Items.Add(this.refreshButton);
      form.ToolStrip.Items.Add(this.processButton);
      form.ToolStrip.Items.Add(this.cancelButton);
      form.Panel.Controls.Add(this.control);
    }

    public void Leave(MainForm form)
    {
      // HACK: Damn deep water.
      form.ToolStrip.Items.Remove(this.separator);
      form.ToolStrip.Items.Remove(this.refreshButton);
      form.ToolStrip.Items.Remove(this.processButton);
      form.ToolStrip.Items.Remove(this.cancelButton);
      form.Panel.Controls.Clear();
    }

    private void RefreshButtonClick(object sender, System.EventArgs e)
    {
      FileExplorer.Instance.Scan();
    }

    private void ProcessButtonClick(object sender, System.EventArgs e)
    {
      this.newMetaDataTable.Clear();

      BindingSource bs = this.control.OriginalBindingSource;
      if (bs.Current == null)
      {
        bs.MoveFirst();
        if (bs.Current == null)
        {
          MessageBox.Show("Wtf, gimme some data to analyse first.");
          return;
        }
      }

      DataRowView view = (DataRowView)bs.Current;
      MetaDataRow row = (MetaDataRow)view.Row;
      MetaDataFile file = row.File;

      // HACK: Fat and ungly.
      TrackQueryParameters parameters = new TrackQueryParameters();
      parameters.Puid = this.genPuid.GetPuid(file.FileData.FullName);
      foreach (Track track in Track.Query(parameters))
      {
        foreach (Release release in track.GetReleases())
        {
          // TODO: Convert to year and add to the metadata.
          ////string date = String.Empty;
          ////ReadOnlyCollection<Event> events = release.GetEvents();
          ////if (events.Count > 0)
          ////{
          ////  date = events[0].Date;
          ////}

          MetaData metaData = new MetaData();
          metaData.Artist = track.GetArtist();
          metaData.Title = track.GetTitle();
          metaData.Duration = track.GetDuration();

          metaData.Album = release.GetTitle();
          metaData.Track = (uint?)track.GetTrackNumber(release);
          metaData.TrackCount = (uint?)release.GetTracks().Count;

          MetaDataRow newRow =
            (MetaDataRow)this.newMetaDataTable.NewRow();
          newRow.Set(metaData);
          this.newMetaDataTable.Rows.Add(newRow);
        }
      }

      this.newMetaDataTable.AcceptChanges();
    }

    private void CancelButtonClick(object sender, System.EventArgs e)
    {
      throw new Exception("The method or operation is not implemented.");
    }
  }
}
